package com.collect.cameroon.zhiScene.zhiCollection.zhiDetail;

import android.content.Context;

import com.collect.cameroon.zhiBase.BasePresenter;
import com.collect.cameroon.zhiBase.BaseSubscriber;
import com.collect.cameroon.zhiBean.zhiReceive.ZHIRCaseDetailEntity;
import com.collect.cameroon.zhiBean.zhiReceive.ZHIRPartRepayListEntity;
import com.collect.cameroon.zhiBean.zhiReceive.ZHIRPhoneBookEntity;
import com.collect.cameroon.zhiBean.zhiReceive.ZHIRPhoneBookSmsEntity;
import com.collect.cameroon.zhiBean.zhiReceive.ZHIRRecTagEntity;
import com.collect.cameroon.zhiBean.zhiReceive.ZHIRRepaymentLinkEntity;
import com.collect.cameroon.zhiBean.zhiReceive.ZHIRSmsBothContactsEntity;
import com.collect.cameroon.zhiBean.zhiSend.ZHISCaseDetailEntity;
import com.collect.cameroon.zhiBean.zhiSend.ZHISCaseRecordEntity;
import com.collect.cameroon.zhiBean.zhiSend.ZHISCreateDunVirtualAccountEntity;
import com.collect.cameroon.zhiBean.zhiSend.ZHISPartRepayListEntity;
import com.collect.cameroon.zhiBean.zhiSend.ZHISRepaymentLinkEntity;
import com.collect.cameroon.zhiBean.zhiSend.ZHISSendSmsEntity;
import com.collect.cameroon.zhiNetApi.ZHIHttpApiProvider;
import com.collect.cameroon.zhiNetApi.ZHIHttpResponse;
import com.collect.cameroon.zhiUtils.RxSchedulers;

import java.util.ArrayList;

import javax.inject.Inject;

public class ZHIDetailPresenter extends BasePresenter<ZHIDetailContract.View> implements ZHIDetailContract.Presenter {

    @Inject
    public ZHIDetailPresenter() {

    }

    @Override
    public void getCaseDetailV2(Context context, final ZHISCaseDetailEntity zhiSCaseDetailEntity) {
        BaseSubscriber<ZHIHttpResponse<ZHIRCaseDetailEntity>> subscriber = new
                BaseSubscriber<ZHIHttpResponse<ZHIRCaseDetailEntity>>(context, this, true) {
                    @Override
                    protected void onSuccess(Object object) {
                        try {
                            ZHIRCaseDetailEntity caseDetailEntity = (ZHIRCaseDetailEntity) object;
                            mView.onGetCaseDetailV2(caseDetailEntity);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    protected void onFailure(int code, String messge) {
                        showMessage(messge);
                        mView.onGetErrorInfo(code + "", messge);
                    }
                };
        ZHIHttpApiProvider.getApi().caseInfoV2(zhiSCaseDetailEntity)
                .compose(RxSchedulers.applySchedulers())
                .compose(mView.bindToLife())
                .subscribe(subscriber);
    }

    @Override
    public void caseRecord(Context context, ZHISCaseRecordEntity zhiSCaseRecordEntity) {
        BaseSubscriber<ZHIHttpResponse> subscriber = new
                BaseSubscriber<ZHIHttpResponse>(context, this, true) {
                    @Override
                    protected void onSuccess(Object object) {
                        try {
                            mView.onCaseRecord();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    protected void onFailure(int code, String messge) {
                        showMessage(messge);
                        mView.onGetErrorInfo(code + "", messge);
                    }
                };
        ZHIHttpApiProvider.getApi().caseRecordV2(zhiSCaseRecordEntity)
                .compose(RxSchedulers.<ZHIHttpResponse>applySchedulers())
                .compose(mView.<ZHIHttpResponse>bindToLife())
                .subscribe(subscriber);
    }

    @Override
    public void caseRecordForWhatsapp(Context context, final ZHISCaseRecordEntity zhiSCaseRecordEntity, final String content) {
        BaseSubscriber<ZHIHttpResponse> subscriber = new
                BaseSubscriber<ZHIHttpResponse>(context, this, true) {
                    @Override
                    protected void onSuccess(Object object) {
                        try {
                            mView.onCaseRecordForWhatsapp(zhiSCaseRecordEntity.getContactMobile(), content);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    protected void onFailure(int code, String messge) {
                        showMessage(messge);
                        mView.onGetErrorInfo(code + "", messge);
                    }
                };
        ZHIHttpApiProvider.getApi().caseRecordV2(zhiSCaseRecordEntity)
                .compose(RxSchedulers.<ZHIHttpResponse>applySchedulers())
                .compose(mView.<ZHIHttpResponse>bindToLife())
                .subscribe(subscriber);
    }

    @Override
    public void sendTagSms(Context context, ZHISSendSmsEntity zhisSendSmsEntity) {
        BaseSubscriber<ZHIHttpResponse> subscriber = new
                BaseSubscriber<ZHIHttpResponse>(context, this, true) {
                    @Override
                    protected void onSuccess(Object object) {
                        try {
                            mView.onSendTagSms();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    protected void onFailure(int code, String messge) {
                        showMessage(messge);
                        mView.onGetErrorInfo(code + "", messge);
                    }
                };
        ZHIHttpApiProvider.getApi().sendTagSms(zhisSendSmsEntity)
                .compose(RxSchedulers.<ZHIHttpResponse>applySchedulers())
                .compose(mView.<ZHIHttpResponse>bindToLife())
                .subscribe(subscriber);
    }


    @Override
    public void sendTagSmsV2(Context context, ZHISSendSmsEntity zhiSSendSmsEntity) {
        BaseSubscriber<ZHIHttpResponse> subscriber = new
                BaseSubscriber<ZHIHttpResponse>(context, this, true) {
                    @Override
                    protected void onSuccess(Object object) {
                        try {
                            mView.onSendTagSms();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    protected void onFailure(int code, String messge) {
                        showMessage(messge);
                        mView.onGetErrorInfo(code + "", messge);
                    }
                };
        ZHIHttpApiProvider.getApi().sendTagSmsV2(zhiSSendSmsEntity)
                .compose(RxSchedulers.<ZHIHttpResponse>applySchedulers())
                .compose(mView.<ZHIHttpResponse>bindToLife())
                .subscribe(subscriber);
    }

    @Override
    public void getRepayLink(Context context, ZHISRepaymentLinkEntity zhiSRepaymentLinkEntity) {
        BaseSubscriber<ZHIHttpResponse<ZHIRRepaymentLinkEntity>> subscriber = new
                BaseSubscriber<ZHIHttpResponse<ZHIRRepaymentLinkEntity>>(context, this, true) {
                    @Override
                    protected void onSuccess(Object object) {
                        try {
                            ZHIRRepaymentLinkEntity zhirRepaymentLinkEntity = (ZHIRRepaymentLinkEntity) object;
                            mView.onGetRepayLink(zhirRepaymentLinkEntity);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    protected void onFailure(int code, String messge) {
                        showMessage(messge);
                        mView.onGetErrorInfo(code + "", messge);
                    }
                };
        ZHIHttpApiProvider.getApi().getRepayLink(zhiSRepaymentLinkEntity)
                .compose(RxSchedulers.<ZHIHttpResponse<ZHIRRepaymentLinkEntity>>applySchedulers())
                .compose(mView.<ZHIHttpResponse<ZHIRRepaymentLinkEntity>>bindToLife())
                .subscribe(subscriber);
    }

    @Override
    public void sendTagSmsLink(Context context, ZHISSendSmsEntity zhisSendSmsEntity) {
        BaseSubscriber<ZHIHttpResponse> subscriber = new
                BaseSubscriber<ZHIHttpResponse>(context, this, true) {
                    @Override
                    protected void onSuccess(Object object) {
                        try {
                            mView.onSendTagSmsLink();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    protected void onFailure(int code, String messge) {
                        showMessage(messge);
                        mView.onGetErrorInfo(code + "", messge);
                    }
                };
        ZHIHttpApiProvider.getApi().sendTagSmsLink(zhisSendSmsEntity)
                .compose(RxSchedulers.<ZHIHttpResponse>applySchedulers())
                .compose(mView.<ZHIHttpResponse>bindToLife())
                .subscribe(subscriber);
    }

    @Override
    public void createVirtualAccount(Context context, ZHISCreateDunVirtualAccountEntity zhisCreateDunVirtualAccountEntity) {
        BaseSubscriber<ZHIHttpResponse> subscriber = new
                BaseSubscriber<ZHIHttpResponse>(context, this, true) {
                    @Override
                    protected void onSuccess(Object object) {
                        try {
                            mView.onCreateVirtualAccount();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    protected void onFailure(int code, String messge) {
                        showMessage(messge);
                        mView.onGetErrorInfo(code + "", messge);
                    }
                };
        ZHIHttpApiProvider.getApi().createDunVirtualAccount(zhisCreateDunVirtualAccountEntity)
                .compose(RxSchedulers.<ZHIHttpResponse>applySchedulers())
                .compose(mView.<ZHIHttpResponse>bindToLife())
                .subscribe(subscriber);
    }

    @Override
    public void getPartRepayList(Context context, ZHISPartRepayListEntity zhiSPartRepayListEntity) {
        BaseSubscriber<ZHIHttpResponse<ArrayList<ZHIRPartRepayListEntity>>> subscriber = new
                BaseSubscriber<ZHIHttpResponse<ArrayList<ZHIRPartRepayListEntity>>>(context, this, true) {
                    @Override
                    protected void onSuccess(Object object) {
                        try {
                            ArrayList<ZHIRPartRepayListEntity> arrayList = (ArrayList<ZHIRPartRepayListEntity>) object;
                            mView.onGetPartRepayList(arrayList);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    protected void onFailure(int code, String messge) {
                        showMessage(messge);
                        mView.onGetErrorInfo(code + "", messge);
                    }
                };
        ZHIHttpApiProvider.getApi().partRepayList(zhiSPartRepayListEntity)
                .compose(RxSchedulers.applySchedulers())
                .compose(mView.bindToLife())
                .subscribe(subscriber);
    }

    @Override
    public void getSmsBothContacts(Context context, ZHISCaseDetailEntity zhiSCaseDetailEntity) {
        BaseSubscriber<ZHIHttpResponse<ZHIRSmsBothContactsEntity>> subscriber = new
                BaseSubscriber<ZHIHttpResponse<ZHIRSmsBothContactsEntity>>(context, this, true) {
                    @Override
                    protected void onSuccess(Object object) {
                        try {
                            ZHIRSmsBothContactsEntity zhirSmsBothContactsEntity = (ZHIRSmsBothContactsEntity) object;
                            ArrayList<ZHIRPhoneBookSmsEntity> arrayList = zhirSmsBothContactsEntity.getBothContacts();
                            mView.onGetSmsBothContacts(arrayList);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    protected void onFailure(int code, String messge) {
                        showMessage(messge);
                        mView.onGetErrorInfo(code + "", messge);
                    }
                };
        ZHIHttpApiProvider.getApi().getSmsBothContacts(zhiSCaseDetailEntity)
                .compose(RxSchedulers.applySchedulers())
                .compose(mView.bindToLife())
                .subscribe(subscriber);
    }

    @Override
    public void getDunRecords(Context context, ZHISCaseDetailEntity zhiSCaseDetailEntity) {
        BaseSubscriber<ZHIHttpResponse<ArrayList<ZHIRRecTagEntity>>> subscriber = new
                BaseSubscriber<ZHIHttpResponse<ArrayList<ZHIRRecTagEntity>>>(context, this, true) {
                    @Override
                    protected void onSuccess(Object object) {
                        try {
                            ArrayList<ZHIRRecTagEntity> arrayList = (ArrayList<ZHIRRecTagEntity>) object;
                            mView.onGetDunRecords(arrayList);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    protected void onFailure(int code, String messge) {
                        showMessage(messge);
                        mView.onGetErrorInfo(code + "", messge);
                    }
                };
        ZHIHttpApiProvider.getApi().getDunRecords(zhiSCaseDetailEntity)
                .compose(RxSchedulers.applySchedulers())
                .compose(mView.bindToLife())
                .subscribe(subscriber);
    }

    @Override
    public void getDunFollows(Context context, ZHISCaseDetailEntity zhiSCaseDetailEntity) {
        BaseSubscriber<ZHIHttpResponse<ArrayList<ZHIRPhoneBookEntity>>> subscriber = new
                BaseSubscriber<ZHIHttpResponse<ArrayList<ZHIRPhoneBookEntity>>>(context, this, true) {
                    @Override
                    protected void onSuccess(Object object) {
                        try {
                            ArrayList<ZHIRPhoneBookEntity> arrayList = (ArrayList<ZHIRPhoneBookEntity>) object;
                            mView.onGetDunFollows(arrayList);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    protected void onFailure(int code, String messge) {
                        showMessage(messge);
                        mView.onGetErrorInfo(code + "", messge);
                    }
                };
        ZHIHttpApiProvider.getApi().getDunFollows(zhiSCaseDetailEntity)
                .compose(RxSchedulers.applySchedulers())
                .compose(mView.bindToLife())
                .subscribe(subscriber);
    }
}
